<?php

namespace App\Http\Middleware;

use App\Models\AdminPermission;
use Closure;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\View;

class Permission
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        //获取所有权限数据
        $data = $this->permissionMenu();
        //dd($data);
        if(!$data['flag']){
            abort(403);
        }

        //数据绑定模板
        View::share('menus',$data['menus']);
        View::share('now_nav',$data['now_nav']);

        return $next($request);
    }

    // 获取所有权限
    private function allPermission()
    {
        $all_permission = AdminPermission::orderBy('sort','asc')->get();
        $all_permission = !empty($all_permission) ? $all_permission->toArray() : [];

        return $all_permission;
    }

    //所有权限菜单
    private function permissionMenu()
    {
        $data = [];
        //当前路由别名
        $currentRoute = Route::currentRouteName();
        //获取所有权限及菜单 并缓存
        if(!Cache::has('all_permission')){
            $all_permission = $this -> allPermission();
            Cache::store('file')->forever('all_permission',$all_permission);
        }
        $all_permission = Cache::store('file')->get('all_permission');
        //用户判断用户当前路由是否有权限标识
        $perm_flag = false;
        //菜单
        $menus = [];
        //当前路径
        $now_nav = [];
        foreach ($all_permission as $k=>$v){
            if($v['rule'] == $currentRoute){
                $perm_flag = true;
                $now_nav[] = $v;
            }

            if($v['is_menu'] == 1){
                $menus[] = $v;
            }
        }
        //后去当前菜单面包屑导航
        foreach ($now_nav as $k_n => $v_n){
            foreach ($menus as $k => $v){
                if($v_n['pid'] == $v['id']){
                    array_unshift($now_nav,$v);
                    break;
                }
            }
        }
        //缓存菜单
        if(!Session::has('admin_menus')){
            $menus = $this->treeMenus($menus);
            Session::put('admin_menus', $menus);
        }
        //uid =1 为超级管理员
        if(Session::get('uid') == 1){
            $perm_flag = true;
        }
        $menus = Session::get('admin_menus');
        $data['menus'] = $menus;
        $data['now_nav'] = $now_nav;
        $data['flag'] = $perm_flag;

        return $data;
    }

    //菜单转成tree
    protected function treeMenus($menus)
    {
        $first = [];  //顶级菜单数组
        $temp = [];   //剩下菜单数组,为了减少循环
        foreach ($menus as $k=>$v){
            if($v['pid'] == 0){
                $first[] = $v;
            }else{
                $temp[] = $v;
            }
        }
        unset($menus);
        foreach ($first as $k_r => $v_r){
            foreach ($temp as $k_t => $v_t){
                if($v_r['id'] == $v_t['pid']){
                    $first[$k_r]['child'][] = $v_t;
                }
            }
        }
        return $first;
    }


}
